using System;
using System.Collections.Generic;
using System.Reflection;
using Bingo.API.Localization;
using CatLib;
using CatLib.API.Translation;
using QFramework;
using UnityEngine;
using Resources = Bingo.Facades.Resources;

namespace Bingo.Localization
{
    public class LocalizationSystem : ILocalization
    {
        private ITranslator _translator;
        private Dictionary<string, Localization> langDic;

        private List<string> _supportLanguage;

        public void Load()
        {
            _supportLanguage = GetSupportLanguage();

            UnityEngine.TextAsset textAsset =
                Resources.LoadSync<UnityEngine.TextAsset>(BAssetBundle.Localization.BundleName,
                    BAssetBundle.Localization.LOCALIZATIONCONFIG);
            _translator = App.Make<ITranslator>();
            InitLocalizationDic(textAsset.text);
            _translator.SetResources(new LocalTranslateResources());
            _translator.SetLocale(_supportLanguage[0]);
        }

        private List<string> GetSupportLanguage()
        {
            FieldInfo[] fieldInfos = typeof(Localization).GetFields();
            List<string> languages = new List<string>();
            foreach (var info in fieldInfos)
            {
                if (info.Name != "Id")
                {
                    languages.Add(info.Name);
                }
            }

            return languages;
        }

        private void InitLocalizationDic(string langText)
        {
            langDic = new Dictionary<string, Localization>();
            foreach (string str in langText.Split(new[] {"\n"}, StringSplitOptions.None))
            {
                try
                {
                    string str2 = str.Trim();
                    if (str2 == "")
                    {
                        continue;
                    }

                    Localization t = str2.FromJson<Localization>();
                    this.langDic.Add(t.Id.ToString(), t);
                }
                catch (Exception e)
                {
                    throw new Exception($"parser json fail: {str}", e);
                }
            }
        }

        public string GetLocalText(string key, string locale)
        {
            if (langDic.ContainsKey(key))
            {
                Localization localization = langDic[key];
                return typeof(Localization).GetField(locale).GetValue(localization).ToString();
            }
            else
            {
                Log.E("Key : " + key + ", not found");
                return string.Empty;
            }
        }

        public string GetText(string key)
        {
            string text = _translator.GetBy(key, _translator.GetLocale());
            Debug.Log("----> text : " + text);
            return text;
        }

        public void ChangeLanguage()
        {
            string currentLanguage = _translator.GetLocale();
            int index = 0;
            int count = _supportLanguage.Count;
            for (int i = 0; i < count; i++)
            {
                if (_supportLanguage[i] == currentLanguage)
                {
                    index = i;
                    break;
                }
            }
            
            currentLanguage = _supportLanguage[(index + 1) % count];
            _translator.SetLocale(currentLanguage);
            App.Trigger(EventConst.EVENT_CHANGE_LANGUAGE);
        }
    }
}